package com.eternal.util.user.service;

import com.eternal.Exception.LoginException;
import com.eternal.Exception.ResultEnum;
import com.eternal.util.user.vo.UserVo;
import lombok.extern.java.Log;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;

/**
 * 用户操作工具类
 *
 * @Class UserUtil
 * @Description: TODO
 * @Author Eternal
 * @Version
 * @date 2018/9/23 17:05
 */
@Log
public final class UserUtil {



    /**
     * <p>  获取用户缓存信息 </p>
     *
     * @Description: TODO
     * @Param
     * @Return
     * @Author Eternal
     * @Date 2018/9/23 17:21
     */
    public static UserVo getUser() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Object object = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (null == object) {
            throw new LoginException(ResultEnum.USERINFO_FAILURE);
        }
        SimplePrincipalCollection simplePrincipalCollection = (SimplePrincipalCollection) object;
        UserVo userVo = (UserVo) simplePrincipalCollection.getPrimaryPrincipal();
        return userVo;
    }


}
